package com.example.blog.controller;

import com.example.blog.constant.Constants;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.Result;
import com.example.blog.service.BlogService;
import com.example.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 获取博客列表
     * @return
     */
    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        log.info("返回博客列表");
        return blogService.selectAll();
    }

    /**
     * 获取博客详情
     * @param blogId
     * @param request
     * @return
     */
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request){
        log.info("getBlogDetail, 获取到参数 blogId: {}", blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        // 获取当前登录用户id
        String jwtToken = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer loginUser = JwtUtils.getIdByToken(jwtToken);
        if(loginUser != null && blogDetail.getUserId() == loginUser){
            blogDetail.setLoginUser(1);
        }
        return  blogDetail;
    }

    /**
     * 发布博客
     * @param blogInfo
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public Result addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request){
        log.info("发布博客, 获取参数 blogInfo:{}", blogInfo);
        //参数校验
        if(!StringUtils.hasLength(blogInfo.getTitle())
                || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("标题或内容为空");
        }
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户id
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null || userId <= 0){
            return Result.fail("当前博客作者不为登录用户!");
        }
        blogInfo.setUserId(userId);
        int result = blogService.insertBlog(blogInfo);
        if(result <= 0){
            return Result.fail("发布博客失败!");
        }
        log.info("博客发布成功");
        return Result.success("发布成功!");
    }

    /**
     * 删除博客
     * @param blogId
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer blogId){
        log.info("delete 接收参数blogId: {}", blogId);
        //参数校验
        if(blogId <= 0){
            return Result.fail("博客Id有误!");
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        int ret = blogService.updateBlog(blogInfo);
        if(ret <= 0){
            return Result.fail("删除失败!");
        }
        return Result.success("删除成功!");
    }

    /**
     * 更新博客
     * @param blogInfo
     * @return
     */
    @RequestMapping("/update")
    public Result update(BlogInfo blogInfo){
        log.info("update 获取到参数blogInfo:{}", blogInfo);
        //参数校验
        if(!StringUtils.hasLength(blogInfo.getTitle())
                || blogInfo.getId() <= 0
                || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("博客Id有误、标题或内容为空");
        }
        Integer ret = blogService.updateBlog(blogInfo);
        if(ret <= 0){
            return Result.fail("更新失败!");
        }
        return Result.success("更新成功!");
    }

}
